<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 
        循环录入5个人的年龄并计算平均年龄,如果录入的数据出现负数或大于100的数,立即停止输入并报错.
     -->
</head>

<body>

</body>
<script>
    let count = 5,
        ageArr = [0];
    for (let i = 0; i < count; i++) {
        ageArr[i] = parseInt(prompt('请输入第' + (i + 1 + '个人的年龄')));
        if (ageArr[i] > 100 || ageArr[i] < 0) {
            alert('⚠输入错误！');
            break;
        }
    }
    alert(count + '人的平均年龄为：' + getAverageAge(ageArr) + '岁');

    function getAverageAge(arr) {
        let len = arr.length;
        let ageTotal = 0;
        for (let i = 0; i < len; i++) {
            ageTotal += arr[i];
        }
        return ageTotal / len;
    }
</script>

</html>